package cn.christ.com.mobilesafe.common.utils.downloadapputils;

import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;

import cn.christ.com.mobilesafe.MyApp;
import cn.christ.com.mobilesafe.common.utils.PackageUtil;

/**
 * Created by Administrator on 2018/7/14.
 */

public class DownloadManagerUtil {

    private Context context;
    private String mimeType;
    private static DownloadManager dm;
    private String DOWNLOADID = "downloadid";

    public DownloadManagerUtil(String mimeType) {
        this.context = MyApp.context;
        dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        this.mimeType = mimeType;
    }

    /**
     * 用系统的downloadManager下载,设置
     *
     * @param url   下载的url
     * @param title 下载的标题
     * @param desc  下载的描述
     * @return 返回下载的进度条
     */
    public long downLoad(String url, String title, String desc) {
        //url转化成uri
        Uri uri = Uri.parse(url);
        DownloadManager.Request req = new DownloadManager.Request(uri);
        //设置wifi下进行更新
        req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        //下载中和下载完后都显示通知栏
        req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        //使用系统默认的下载路径 此处为应用内 /android/data/pacckags，所以要兼容7.0
        req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title);
        //通知栏标题
        req.setTitle(title);
        //通知栏描述信息
        req.setDescription(desc);
        //设置类型为.apk
        req.setMimeType(mimeType);
        //获取下载任务ID,开始下载
        DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
//        long enqueue = dm.enqueue(req);
//        SPUtils.put(DOWNLOADID, enqueue);
        return dm.enqueue(req);
    }


    /**
     * 获取到id的下载状态
     *
     * @param downloadId
     * @return
     */
    public static int getDownloadStatus(long downloadId) {
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadId);
        Cursor c = dm.query(query);
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    //获取当前id的下载状态
                    return c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭资源
                c.close();
            }
        }
        return -1;
    }

    /**
     * 获取到apk的uri地址
     *
     * @param id
     * @return
     */
    public Uri getUriForDownloadedFile(long id) {
        return dm.getUriForDownloadedFile(id);
    }


    /**
     * 下载前先移除前一个任务，防止重复下载
     *
     * @param downloadId
     */
    public void clearCurrentTask(long downloadId) {
        try {
            dm.remove(downloadId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取到app的信息
     *
     * @param path
     * @return
     */
    public PackageInfo getApkInfo(String path) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(path, PackageManager.GET_ACTIVITIES);
            if (info != null) {
                return info;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 如果当前版本号小于apk的版本号则返回true,代表需要更新
     *
     * @return
     */
    public boolean compareApkCode(PackageInfo info) {
        if (info == null) {
            return false;
        }
        int versionCode = PackageUtil.getVersionCode();
        if (info.versionCode > versionCode) {
            return true;
        }
        return false;

    }

}
